<template>
  <div class="table">
    <el-row>
      <el-col :span="8">
        <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getUserList">
          <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
        </el-input>
      </el-col>
      <el-col :span="4" :offset="1">
        <el-button type="primary">添加</el-button>
      </el-col>
    </el-row>
    <el-table :data="tableData" style="width: 100%" stripe border>
      <el-table-column label="id" width="100" prop="id" align="center"></el-table-column>
      <el-table-column label="姓名" width="120" prop="username" align="center"></el-table-column>
      <el-table-column label="身份" width="150" prop="role_name" align="center"></el-table-column>
      <el-table-column label="电话" width="180" prop="mobile" align="center"></el-table-column>
      <el-table-column label="邮箱" width="250" prop="email"></el-table-column>
      <el-table-column label="状态" width="100" align="center">
        <template v-slot="scope">
          <el-switch
            v-model="scope.row.mg_state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="userStateChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="250">
        <template v-slot="scope">
          <!-- 修改按钮 -->
          <el-tooltip class="item" effect="dark" content="编辑" placement="top" :enterable="false">
            <el-button type="primary" icon="el-icon-edit" @click="edit(scope.row)"></el-button>
          </el-tooltip>
          <!-- 删除按钮 -->
          <el-tooltip class="item" effect="dark" content="删除" placement="top" :enterable="false">
            <el-button type="danger" icon="el-icon-delete"></el-button>
          </el-tooltip>
          <!-- 设置按钮 -->
          <el-tooltip class="item" effect="dark" content="设置" placement="top" :enterable="false">
            <el-button type="warning" icon="el-icon-setting"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[1,2,5,10]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: null,
      //   获取用户列表要的参数
      queryInfo: {
        query: "",
        // 当前页数
        pagenum: 1,
        // 显示多少条
        pagesize: 2
      },
      // total一共多少页
      total:0
    };
  },
  created() {
    this.getUserList();
  },
  // 监听事件
  watch:{
    // 监听搜索框,如果为空，就展示所有数据
    'queryInfo.query':{
      handler(){
        if (this.queryInfo.query=='') {
          this.getUserList()
        }
      },
      deep:true
    }
  },

  methods: {
    // 分页
    // 监听pagesize变化事件
    handleSizeChange(newv) {
      this.queryInfo.pagesize = newv
      this.getUserList();
    },
    // 监听pagenum变化事件
    handleCurrentChange(newvl) {
      this.queryInfo.pagenum = newvl
      this.getUserList();
    },
    // 获取用户列表请求
    getUserList() {
      this.$axios.get("/users", { params: this.queryInfo }).then(res => {
        // console.log(res);
        this.tableData = res.data.data.users;
        this.total = res.data.data.total
      });
    },
    // 监听用户状态改变
    async userStateChange(userinfo){
      await this.$axios.put(`users/${userinfo.id}/state/${userinfo.mg_state}`).then(res=>{
        if (res.status !== 200) {
          userinfo.mg_state = !userinfo.mg_state
          return this.$message.error('更新用户状态失败')
        }else{
          this.$message.success('更新用户状态成功')
        }
        
      })
    },
    // 搜索实现

    // 编辑
    edit(row) {
      console.log(row);
    },
  }
};
</script>
<style scoped>
</style>